﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using PPI.Client.Logic;
using PPI.Client.Logic.Delegate;
using PPI.Client.Utils;
using PPI.Entities;

namespace PPI.Client.Presenter
{
    public interface  ISplashVisit
    {
        string ActionMessage{ set;}
        Int32  ActionProgress{ set;}
        Int32  VisitId{ get;}
        EVendor_Visit_XMissionStructure Visit{ set;}
        void Finish();
        bool IsDownloadImages{ get; set;}

    }


    public class SplashVisitPresenter
    {
        private ISplashVisit m_View;
        private DownloadFile m_Downloader;
        private UpdateBar m_DelegateUpdateProgressBar;
        private Stack<EBaseAssetImage> m_ImagesUrlToDownload;
        private EVendor_Visit_XMissionStructure m_Visit;

        public SplashVisitPresenter(ISplashVisit view)
        {
            m_View = view;
            Initialize();
        }

        private void Initialize()
        {
            try
            {
                m_DelegateUpdateProgressBar = new UpdateBar(UpdateActionProgressBar);
                m_Downloader = new DownloadFile(m_DelegateUpdateProgressBar);
                m_Downloader.FinishDownloadFile += new EventHandler(m_Downloader_FinishDownloadFile);
                DownloadVisit();
                if (m_Visit != null && m_View.IsDownloadImages)
                {
                    PrepareImagesToDownload();
                    DownloadImages();
                }else
                {
                    m_View.Visit = m_Visit;
                    m_View.Finish();
                }

                
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }

        private void DownloadImages()
        {
            m_View.ActionMessage = "Downloading Images ...";
            if(m_ImagesUrlToDownload.Count > 0)
            {
                EBaseAssetImage tmpImage = m_ImagesUrlToDownload.Pop();
                m_Downloader.SetUrl(tmpImage.GetImageUrl(tmpImage.Asset.Id));
                m_Downloader.SetFileDestination(tmpImage.GetTemporaryFolder());
                if (!File.Exists(tmpImage.GetTemporaryFolder()))
                    m_Downloader.StartDownload();
                else 
                    DownloadImages();
            }else
            {
                m_View.ActionMessage = "Finish Downloading images ...";

                foreach (EBaseAssetImage i in m_Visit.Images)
                {
                    if (File.Exists(i.GetTemporaryFolder()))
                        i.ImageFile = new FileInfo(i.GetTemporaryFolder());
                }
                m_View.Visit = m_Visit;
                m_View.Finish();
            }
        }

        private void PrepareImagesToDownload()
        {
            m_View.ActionMessage = "Preparing Images ...";
            m_ImagesUrlToDownload = new Stack<EBaseAssetImage>(m_Visit.Images);
        }

        private void DownloadVisit()
        {
            m_View.ActionMessage = "Downloading Data ...";
            List<EVendor_Visit_XMissionStructure> visits = LVendor_Visit_XMissionStructure.GetServerVisitById(m_View.VisitId);
            if (visits.Count > 0)
                m_Visit = visits[0];
            else
                m_View.ActionMessage = string.Format("Downloading Error in Visit {0}", m_View.VisitId);
            
        }

        private void UpdateActionProgressBar(int percentage)
        {
            m_View.ActionProgress = percentage;
        }

        #region Events
        void m_Downloader_FinishDownloadFile(object sender, EventArgs e)
        {
            try
            {
                DownloadImages();
            }
            catch (Exception ex)
            {
                RegisterError.SaveError(ex);
            }
        }
        #endregion


    }
}
